import 'package:get/get.dart';
import 'package:safetymonitor/app/components/custom_select_function.dart';
import 'package:safetymonitor/app/components/toast_widget.dart';
import 'package:safetymonitor/app/core/http/http.dart';
import 'package:safetymonitor/app/core/utils/interface.dart';
import 'package:safetymonitor/app/data/entities/application/get_dict_all_entity.dart';
import 'package:safetymonitor/app/data/entities/application/preventive_rectification_get_entity.dart';
import 'package:safetymonitor/modules/home/snap_shot_danger/no_completed/controller.dart';

///待验收
class PendingAcceptanceStateController extends GetxController {
  /// 当前选中的标题索引，0：基本信息 1:待验收
  RxInt currentIndex = 0.obs;

  /// 即是详情相应  也可以是待验收请求体
  PreventiveRectificationGetEntity preventiveRectificationGetEntity =
      PreventiveRectificationGetEntity();

  static List<GetDictAllEntity> currencyState = [];
  GetDictAllEntity? dictEntity1;

  ///	隐患上报视频集合[数组]
  List<String>? dangerVideo = [];
  List<String>? dangerImages = [];

  @override
  void onInit() {
    super.onInit();
    initData();
    // if (UserManager.getUserCompanyId() == 100) {
      currencyState.clear();
      final GetDictAllEntity getDictAllEntity = GetDictAllEntity();
      getDictAllEntity.id = '1';
      getDictAllEntity.dictionaryLabel = '合格';
      currencyState.add(getDictAllEntity);
      final GetDictAllEntity getDictAllEntity2 = GetDictAllEntity();
      getDictAllEntity2.id = '2';
      getDictAllEntity2.dictionaryLabel = '不合格';
      currencyState.add(getDictAllEntity2);
    // }
  }
  ///状态
  void selectCurrencyLevelType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '状态',
      dataList: currencyState,
      checkedList: [dictEntity1 ?? GetDictAllEntity()],
      isMultiple: false,
      onCallBack: (value) {
        dictEntity1 = value[0];
         preventiveRectificationGetEntity.acceptanceStatus = int.parse(value[0].getId());
         yanShou= value[0].getShowName();
        update();
      },
    );
  }

  /// 初始化数据
  Future<void> initData() async {
     preventiveRectificationGetEntity.fenCompanyApproveStatus=1;//分公司验收标识
    final res = await HttpUtil()
        .generateGetRequest<PreventiveRectificationGetEntity>(
            Interface.preventiveRectificationGet,
            queryParameters: {
          'id': Get.arguments
        }).execute(isShowLoading: true);
    res.onSuccess((result) {
      if (result.data != null) {
        preventiveRectificationGetEntity =
            result.data ?? PreventiveRectificationGetEntity();
      }
      update();
    });
  }
  /// title选择切换
  void selectTitle(int index) {
    currentIndex.value = index;
  }

  String   yanShou='';
  String    desc='';

  /// 提交
  void submit() async {
    //判断验收 和描述
    if(yanShou.trim().isEmpty){
      AppToast.showWarning('请选择验收');
      return;
    }
    if(desc.trim().isEmpty){
      AppToast.showWarning('请输入验收描述');
      return;
    }
    //加上时间
    preventiveRectificationGetEntity.approvalTime=DateTime.now().millisecondsSinceEpoch;
    final res = await HttpUtil()
        .generatePutRequest(
          Interface.preventiveRectificationUpdate,
          data: preventiveRectificationGetEntity.toJson(),
        )
        .execute(isShowLoading: true);
    res.onSuccess((result) {
      Get.back(result: true);
      AppToast.showWarning('提交成功');
      Get.find<NoCompletedController>().globalKey.currentState?.onRefresh();
    });
  }
}
